package top.swle.austers.controller;

import com.google.gson.Gson;
import top.swle.austers.bean.ResponseBean;
import top.swle.austers.bean.Trip;
import top.swle.austers.mapper.TripMapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/trips")
@RestController
public class TripController {
    @Autowired
    TripMapper tripMapper;
    @RequiresAuthentication
    @PostMapping("/issue_trip")
    public ResponseBean issueTrip(@RequestBody Trip trip){

        try {
            tripMapper.issueTrip(trip);
            return new ResponseBean(1,"successful",null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(-1,"failure",null);
        }

    }

    @RequiresAuthentication
    @PostMapping("/get_in_trip")
    public ResponseBean getInTrip(@RequestBody Trip trip){
        try{
            tripMapper.getInTrip(trip);
            return new ResponseBean(1,"乘坐成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(-1,"乘坐失败，请重试",null);
        }
    }
    @RequiresAuthentication
    @PostMapping("/query_other_trip")
    public ResponseBean queryOtherTrip(@RequestBody Trip trip){
        try {
            List<Trip> trips = tripMapper.queryOtherTrip(trip);
            Gson gson = new Gson();
            String json = gson.toJson(trips);
            System.out.println(trips);
            return new ResponseBean(1,"",json);
        }catch (Exception e){
            return new ResponseBean(-1,"查询失败",null);
        }
    }

}
